Public Class ProductCodeDialog
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtDescription As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtCode As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtBarcode As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblError As System.Web.UI.WebControls.Label
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents RegularExpressionValidator2 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents btnSave As System.Web.UI.WebControls.Button
    Protected WithEvents lblTitle As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private PRODUCT_CODE_PAGEBO_SESSION_NAME As String = "MIMS_WRS.Web.ProductCode.PageBO"

#Region "   Properties"
    Protected ReadOnly Property CustomerID() As Integer
        Get
            Dim id As Integer

            Try
                id = CInt(Request.QueryString("cid"))
            Catch ex As Exception
                id = 0
            End Try

            If Not (id > 0) Then
                Throw New ApplicationException("Invalid or missing parameter")
            End If

            Return id
        End Get
    End Property

    Protected ReadOnly Property ProductCodeID() As Integer
        Get
            Dim id As Integer

            If IsNothing(Request.QueryString("id")) Then
                Return 0
            End If

            Try
                id = CInt(Request.QueryString("id"))

                If Not (id >= 0) Then
                    Throw New Exception
                End If

            Catch ex As Exception
                Throw New ApplicationException("Invalid parameter", ex)
            End Try

            Return id
        End Get
    End Property

    Protected Property PageBO() As CProductCode
        Get
            Return CType(Session(Me.PRODUCT_CODE_PAGEBO_SESSION_NAME), CProductCode)
        End Get
        Set(ByVal Value As CProductCode)
            Session(Me.PRODUCT_CODE_PAGEBO_SESSION_NAME) = Value
        End Set
    End Property

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then

            Dim obj As New CProductCode

            If Me.ProductCodeID > 0 Then
                Try
                    obj.Load(Me.ProductCodeID)

                    With obj
                        txtCode.Text = .Code
                        txtBarcode.Text = .PartOfBarcode
                        txtDescription.Text = .Description
                    End With
                    lblTitle.Text = "Edit Product Code"

                Catch ex As Exception
                    Throw New ApplicationException("Failed to load product code data", ex)
                End Try
            Else
                obj.Init()
                obj.CustomerID = Me.CustomerID
                lblTitle.Text = "Create new Product Code"
            End If

            Me.PageBO = obj
            'Me.CustomerContactCtr.PageBOSessionName = Me.CUSTOMER_CONTACT_PAGEBO_SESSION_NAME

        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            PageBO.Save()
            Me.SetReturnValue()
        Catch ex As Exception
            Me.lblError.Text = ex.Message
        End Try
    End Sub

    Private Sub SetReturnValue()
        Dim s As String = ""
        s += "var obj= new Object();"
        s += String.Format("obj.ID = '{0}';", PageBO.ID)
        s += String.Format("obj.Code = '{0}';", PageBO.Code)
        s += String.Format("obj.PartOfBarcode = '{0}';", PageBO.PartOfBarcode)
        s += String.Format("obj.Description = '{0}';", PageBO.Description)
        s += "window.parent.returnValue = obj;"
        s += "window.parent.close();"
        Response.Write(String.Format("<script>{0}</script", s))
    End Sub

    Private Sub txtCode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCode.TextChanged
        PageBO.Code = txtCode.Text.Trim()
    End Sub

    Private Sub txtBarcode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBarcode.TextChanged
        PageBO.PartOfBarcode = txtBarcode.Text.Trim()
    End Sub

    Private Sub txtDescription_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDescription.TextChanged
        PageBO.Description = txtDescription.Text.Trim()
    End Sub
End Class
